package com.suse.lhw.hms.foodserver.service.Impl;

import com.suse.lhw.hms.foodserver.constant.PackageStatusConst;
import com.suse.lhw.hms.foodserver.dao.*;
import com.suse.lhw.hms.foodserver.domin.*;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.eatAPackage;
import com.suse.lhw.hms.foodserver.pojo.output_POJO.PackageAndAllModels;
import com.suse.lhw.hms.foodserver.pojo.output_POJO.SimpleModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.suse.lhw.hms.foodserver.service.FPackageService;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.foodserver.service.Impl
 * @ClassName: FPackageServiceImpl
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/18 19:30
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/18 19:30
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
public class FPackageServiceImpl implements FPackageService {

    @Resource
    private FPackageMapper fPackageMapper;
    @Resource
    private FPackageToFFoodmodelMapper ptfMapper;
    @Resource
    private FPackageInstanceMapper fPackageInstanceMapper;
    @Resource
    private FPackageInstanceMapFoodmodelsMapper fPackageInstanceMapFoodmodelsMapper;
    @Resource
    private FFoodmodelMapper fFoodmodelMapper;
    @Resource
    private FUnitMapper fUnitMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return fPackageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FPackage record) {
        return fPackageMapper.insert(record);
    }

    @Override
    public int insertSelective(FPackage record) {
        return fPackageMapper.insertSelective(record);
    }

    @Override
    public FPackage selectByPrimaryKey(Integer id) {
        return fPackageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FPackage record) {
        return fPackageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FPackage record) {
        return fPackageMapper.updateByPrimaryKey(record);
    }

    @Override
    public int addADefaultPackage(FPackage fPackage, List<FPackageToFFoodmodel> list) {
        //先创建status为default的套餐
        fPackage.setStatus(PackageStatusConst.defalt);
        fPackageMapper.insertSelective(fPackage);
        //创建套餐对模型的映射s
        for (FPackageToFFoodmodel toFFoodmodel : list) {
            if (ptfMapper.insertSelective(toFFoodmodel) == 0) {
                System.out.println("套餐到模型的映射插入失败！请求原地爆炸！");
                return 0;
            }
        }
        return 1;
    }

    @Override
    public int deleteADefaultPackageByPrimaryKey(int id) {
        //先通过id找出所有于这个套餐绑定的食品模型s
        List<FPackageToFFoodmodel> list = ptfMapper.selectAllPtfByPacakgeId(id);
        if (list.isEmpty()) {
            System.out.println("啥都没找到");
            return 0;
        }
        //删除所有的食品模型映射
        for (FPackageToFFoodmodel fPackageToFFoodmodel : list) {
            if (ptfMapper.deleteByPrimaryKey(fPackageToFFoodmodel.getId()) == 0) {
                System.out.println("删除套餐对食品的映射失败！");
                return 0;
            }
        }
        //删除套餐
        if (fPackageMapper.deleteByPrimaryKey(id) == 0) {
            System.out.println("删除套餐失败！");
            return 0;
        }
        return 1;
    }

    @Override
    public List<FPackage> selectDefualtPackages() {
        return fPackageMapper.selectDefaultPackages(PackageStatusConst.defalt);
    }

    @Override
    public int insertAnUnUploadPackage(FPackage fPackage, List<Integer> fmIds) {
        //先生成一个套餐.规定套餐的状态为未上传
        fPackage.setStatus(PackageStatusConst.unupload);
        int insertResult = fPackageMapper.insertSelective(fPackage);
        if (insertResult == 0)
            return 0;
        //拿到package insert后得到的住建生成食品模型映射
        for (Integer fmId : fmIds) {
            FPackageToFFoodmodel fPackageToFFoodmodel = new FPackageToFFoodmodel(fPackage.getId(), fmId);
            int result = ptfMapper.insertSelective(fPackageToFFoodmodel);
            if (result == 0)
                return 0;
        }
        return 1;
    }

    @Override
    public int deleteAnUnUplodPackage(int pakcageId) {
        //删除一个套餐，首先通过id找出所有映射
        List<FPackageToFFoodmodel> mapList = ptfMapper.selectAllPtfByPacakgeId(pakcageId);
        //删除套餐
        if (fPackageMapper.deleteByPrimaryKey(pakcageId) == 0) {
            return 0;
        }
        for (FPackageToFFoodmodel fPackageToFFoodmodel : mapList) {
            if (ptfMapper.deleteByPrimaryKey(fPackageToFFoodmodel.getId()) == 0)
                System.out.println("删除失败");
        }
        return 1;
    }

    @Override
    public int insertPTMs(int packaeid, List<eatAPackage> ptmList, String userId, int status) {
        //增加套餐实例和套餐中的食品模型实例]
        FPackageInstance instances = new FPackageInstance();
        instances.setFPackageId(packaeid);
        instances.setCreatdate(new Date());
        instances.setPhotourl(fPackageMapper.selectByPrimaryKey(packaeid).getName());
        if (fPackageInstanceMapper.insertSelective(instances) == 0)
            return 0;
        int calories = 0;
        for (eatAPackage fPackageInstanceMapFoodmodels : ptmList) {
            FPackageInstanceMapFoodmodels imm = new FPackageInstanceMapFoodmodels();
            FUnit unit = fUnitMapper.selectByPrimaryKey(fPackageInstanceMapFoodmodels.getUnitId());
            imm.setFPackageInstanceId(instances.getId());
            imm.setFFoodmodelId(fPackageInstanceMapFoodmodels.getModelId());
            imm.setFFoodmodelQuanlity(fPackageInstanceMapFoodmodels.getQuanlity());
            calories += imm.getFFoodmodelQuanlity() * unit.getUnitConsumeCalories();
        }
        //计算出食品套餐实例中的卡路里消耗
        instances.setCalories(calories);
        if (fPackageInstanceMapper.insertSelective(instances) == 0) {
            return 0;
        }
        return 1;
    }

    @Override
    public List<PackageAndAllModels> selectAllDefault(int type) {
        List<PackageAndAllModels> list = new ArrayList<>();
        List<FPackage> packages = fPackageMapper.selectAllByType(type);
        for (FPackage aPackage : packages) {
            PackageAndAllModels packageAndAllModels = new PackageAndAllModels();
            packageAndAllModels.setPackageName(aPackage.getName());
            List<FFoodmodel> foodmodels = fFoodmodelMapper.selectAllByPackageId(aPackage.getId());
            List<SimpleModel> simpleModels = new ArrayList<SimpleModel>();
            for (FFoodmodel foodmodel : foodmodels) {
                simpleModels.add(new SimpleModel(foodmodel.getName(), foodmodel.getImgurl()));
            }
            packageAndAllModels.setList(simpleModels);
            list.add(packageAndAllModels);
        }
        return list;

    }

}
